import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'controller.dart';

/// 活动编辑页面
class ActivityEditPage extends GetView<ActivityEditController> {
  const ActivityEditPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        leading: IconButton(
          onPressed: () => Get.back(),
          icon: const Icon(Icons.arrow_back_ios_new_outlined),
        ),
        title: const Text(
          '编辑活动',
          style: TextStyle(
            color: Color(0xFF333333),
            fontSize: 17,
            fontWeight: FontWeight.w600,
          ),
        ),
        backgroundColor: Colors.white,
        elevation: 0,
        centerTitle: true,
        iconTheme: const IconThemeData(color: Color(0xFF333333)),
      ),
      body: Column(
        children: [
          Expanded(
            child: SingleChildScrollView(
              padding: const EdgeInsets.all(16),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  // 活动标题
                  _buildSectionTitle('活动标题'),
                  const SizedBox(height: 12),
                  Obx(() => TextField(
                    controller: TextEditingController(
                      text: controller.editForm.value.activityTitle,
                    )..selection = TextSelection.fromPosition(
                      TextPosition(offset: controller.editForm.value.activityTitle.length),
                    ),
                    onChanged: (value) {
                      controller.editForm.value.activityTitle = value;
                      controller.editForm.refresh();
                    },
                    maxLength: 50,
                    decoration: InputDecoration(
                      hintText: '请输入活动标题',
                      hintStyle: TextStyle(color: Colors.grey[400], fontSize: 14),
                      filled: true,
                      fillColor: const Color(0xFFF5F5F5),
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(12),
                        borderSide: BorderSide(color: Colors.grey[300]!),
                      ),
                      enabledBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(12),
                        borderSide: BorderSide(color: Colors.grey[300]!),
                      ),
                      focusedBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(12),
                        borderSide: const BorderSide(color: Color(0xFF333333)),
                      ),
                      contentPadding: const EdgeInsets.symmetric(
                        horizontal: 16,
                        vertical: 12,
                      ),
                    ),
                  )),
                  
                  const SizedBox(height: 24),
                  
                  // 活动描述
                  _buildSectionTitle('活动描述'),
                  const SizedBox(height: 12),
                  Obx(() => TextField(
                    controller: TextEditingController(
                      text: controller.editForm.value.activityDetails,
                    )..selection = TextSelection.fromPosition(
                      TextPosition(offset: controller.editForm.value.activityDetails.length),
                    ),
                    onChanged: (value) {
                      controller.editForm.value.activityDetails = value;
                      controller.editForm.refresh();
                    },
                    maxLength: 500,
                    maxLines: 5,
                    decoration: InputDecoration(
                      hintText: '请输入活动描述',
                      hintStyle: TextStyle(color: Colors.grey[400], fontSize: 14),
                      filled: true,
                      fillColor: const Color(0xFFF5F5F5),
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(12),
                        borderSide: BorderSide(color: Colors.grey[300]!),
                      ),
                      enabledBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(12),
                        borderSide: BorderSide(color: Colors.grey[300]!),
                      ),
                      focusedBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(12),
                        borderSide: const BorderSide(color: Color(0xFF333333)),
                      ),
                      contentPadding: const EdgeInsets.all(16),
                    ),
                  )),
                ],
              ),
            ),
          ),
          
          // 底部保存按钮
          _buildBottomButton(),
        ],
      ),
    );
  }

  /// 构建区域标题
  Widget _buildSectionTitle(String title) {
    return Text(
      title,
      style: const TextStyle(
        fontSize: 16,
        fontWeight: FontWeight.w600,
        color: Color(0xFF333333),
      ),
    );
  }

  /// 构建底部按钮
  Widget _buildBottomButton() {
    return Container(
      padding: const EdgeInsets.fromLTRB(16, 12, 16, 24),
      decoration: BoxDecoration(
        color: Colors.white,
        boxShadow: [
          BoxShadow(
            color: Colors.black.withValues(alpha: 0.08),
            blurRadius: 16,
            offset: const Offset(0, -4),
          ),
        ],
      ),
      child: SafeArea(
        child: Obx(() {
          final isSubmitting = controller.isSubmitting.value;
          
          return Container(
            width: double.infinity,
            height: 48,
            decoration: BoxDecoration(
              color: isSubmitting ? Colors.grey[400] : const Color(0xFF333333),
              borderRadius: BorderRadius.circular(24),
              boxShadow: !isSubmitting
                  ? [
                      BoxShadow(
                        color: Colors.black.withValues(alpha: 0.15),
                        blurRadius: 12,
                        offset: const Offset(0, 4),
                      ),
                    ]
                  : null,
            ),
            child: ElevatedButton(
              onPressed: isSubmitting ? null : controller.saveActivity,
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.transparent,
                foregroundColor: Colors.white,
                shadowColor: Colors.transparent,
                elevation: 0,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(24),
                ),
              ),
              child: Text(
                isSubmitting ? '保存中...' : '确定',
                style: const TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.w600,
                ),
              ),
            ),
          );
        }),
      ),
    );
  }
}

